import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import numeral from 'numeral';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import * as menuService from '../../../services/menus';
import * as common from '../../../utils/common';
import INDI_QTZB_DIVI_FHQK from './INDI_QTZB_DIVI_FHQK'
/**恒生理财销售 */
const daoName = "INDI_FANA_SALE";
const ButtonGroup = Button.Group;
const renderNumber = (num) => {
  let value = ''
  if (num) {
    value = numeral(num).format('0,0')
  }
  return value
};
const columns = [
  { title: '产品代码', width: 160, sorter: true, nowrap: true, caption: "Cpdm", key: 'Cpdm', dispaly: true, filter: true },
  { title: '产品名称', width: 180, sorter: true, nowrap: true, caption: "Khmc", key: 'Khmc', dispaly: true, filter: true },
  { title: '类型', width: 210, sorter: true, nowrap: true, caption: "Lx", key: 'Lx', dispaly: true, filter: true },
  { title: '客户名称', width: 120, sorter: true, dataIndex: 'KHMC', key: 'KHMC', dispaly: true, filter: true },
  { title: '交易金额', width: 110, sorter: true, dataIndex: 'Jyje', key: 'Jyje', dispaly: true, filter: true },
  { title: '确认份额', width: 110, sorter: true, dataIndex: 'Qrfe', key: 'Qrfe', dispaly: true, filter: true },
  { title: '交易日期', width: 120, sorter: true, dataIndex: "Jyrq", key: 'Jyrq', dispaly: true, filter: true },
  { title: '确认日期', width: 120, sorter: true, dataIndex: "Qrrq", key: 'Qrrq', dispaly: true, filter: true },
  { title: '证件号码', width: 180, sorter: true, dataIndex: 'Zjhm', key: 'Zjhm', dispaly: true, filter: true },
  { title: '流水号', width: 210, sorter: true, dataIndex: 'Lsh', key: 'Lsh', dispaly: true, filter: true },
  { title: '交易代码', width: 120, sorter: true, dataIndex: 'Jydm', key: 'Jydm', dispaly: true, filter: true },
  { title: '交易机构号', width: 130, sorter: true, dataIndex: 'Jyjgh', key: 'Jyjgh', dispaly: true, filter: true },
  { title: '归属机构号', width: 130, sorter: true, dataIndex: 'Gsjgh', key: 'Gsjgh', dispaly: true, filter: true },
  { title: '客户经理1', width: 180, sorter: false, dataIndex: 'YG1', key: 'YG1', dispaly: true, filter: false },
  { title: '分配比例1(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB1', render: data => renderNumber(data.ZB1), key: 'ZB1', dispaly: true, filter: false },
  { title: '客户经理2', width: 180, sorter: false, dataIndex: 'YG2', key: 'YG2', dispaly: true, filter: false },
  { title: '分配比例2(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB2', render: data => renderNumber(data.ZB2), key: 'ZB2', dispaly: true, filter: false },
  { title: '客户经理3', width: 180, sorter: false, dataIndex: 'YG3', key: 'YG3', dispaly: true, filter: false },
  { title: '分配比例3(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB3', render: data => renderNumber(data.ZB3), key: 'ZB3', dispaly: true, filter: false },
  { title: '客户经理4', width: 180, sorter: false, dataIndex: 'YG4', key: 'YG4', dispaly: true, filter: false },
  { title: '分配比例4(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB4', render: data => renderNumber(data.ZB4), key: 'ZB4', dispaly: true, filter: false },
  { title: '客户经理5', width: 180, sorter: false, dataIndex: 'YG5', key: 'YG5', dispaly: true, filter: false },
  { title: '分配比例5(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB5', render: data => renderNumber(data.ZB5), key: 'ZB5', dispaly: true, filter: false },
];
const controls = [
  { name: 'KHMC', required: true, type: 'text', label: '客户名称', grid: 12 / 24 },
  { name: 'Jyje', required: true, type: 'number', label: '交易金额', grid: 12 / 24 },
  { name: 'Qrfe', required: true, type: 'number', label: '确认份额', grid: 12 / 24 },
  { name: 'Jyrq', required: true, type: 'text', label: '交易日期', grid: 12 / 24 },
  { name: 'Qrrq', required: true, type: 'text', label: '确认日期', grid: 12 / 24 },
  { name: 'Zjhm', required: true, type: 'text', label: '证件号码', grid: 12 / 24 },
  { name: 'Lsh', required: true, type: 'text', label: '流水号', grid: 12 / 24 },
  { name: 'Jydm', required: true, type: 'text', label: '交易代码', grid: 12 / 24 },
  { name: 'Jyjgh', required: true, type: 'text', label: '交易机构号', grid: 12 / 24 },
  { name: 'Gsjgh', required: true, type: 'text', label: '归属机构号', grid: 12 / 24 },
  { name: 'Lx', required: true, type: 'text', label: '类型', grid: 12 / 24 },
  { name: 'Cpdm', required: true, type: 'text', label: '产品代码', grid: 12 / 24 },
  { name: 'Khmc', required: true, type: 'text', label: '产品名称', grid: 12 / 24 },
];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class INDI_FANA_SALE extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }

    });
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20 }
    });
    this.loadOptionData();
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  showModelHandler(isAdd) {
    let key = null
    if (!isAdd) {
      const len = this.props.selectedRowKeys.length;
      if (len === 0) {
        message.info('请选择要认定的数据行！');
        return;
      }
      key = this.props.selectedRowKeys[len - 1]
    }
    this.props.dispatch({
      type: `${daoName}/openEdit`,
      payload: { key }
    });
  }
  findData() {
    const form = this.refs.findForm.getForm();
    let nyc = '';
    let nym = '';
    let csrq = "";
    let jsrq = '';
    let isOK = true;
    form.validateFields((err, values) => {
      if (!err) {
        if (values.YF === undefined || values.YF === '' || values.YF === null) {
          message.info('请选择年月！');
          isOK = false;
        } else {
          let date = new Date(values.YF.get('year'), values.YF.get('month'), 1, 8, 0, 0);
          let year = date.getFullYear();
          let month = date.getMonth() + 1;
          let day = new Date(year, month, 0);
          if (month < 10) {
            month = '0' + month;
          }
          nym = year + '-' + month + '-' + day.getDate();
          jsrq = nym.substring(0, 4) + nym.substring(5, 7) + nym.substring(8, 10)
          nyc = datetime.format(date, "yyyy-MM-dd");
          csrq = nyc.substring(0, 4) + nyc.substring(5, 7) + nyc.substring(8, 10)
        }
      }
    });
    if (!isOK) {
      return;
    }
    let whereItems = [];
    let WhereCauses = [];
    whereItems.push({
      FieldName: "Qrrq",
      Operator: ">=",
      FieldValues: csrq
    }, {
        FieldName: "Qrrq",
        Operator: "<=",
        FieldValues: jsrq
      });
    WhereCauses.push({
      WhereItems: whereItems,
      RelationSign: "AND"
    })
    let seeks = WhereCauses;
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, seeks }
    });
  }
  render() {
    let { showEdit, record, permID, popedomTypes } = this.props;
    const pTypes = popedomTypes[permID]
    let data = [{ value: '1', label: '查询', onClick: this.findData.bind(this) }]
    if (pTypes.indexOf('6') !== -1) {
      let button = { value: '2', label: '业绩认定', onClick: this.showModelHandler.bind(this, false) }
      data.push(button)
    }
    const controlsFilter = [
      { name: 'YF', required: false, type: 'month', label: '年月', grid: 5 / 24, layout: { labelSpan: 8, wrapperSpan: 14 } },
      { name: 'CX', required: false, type: 'button-group', data: data, grid: 5 / 24 },
    ];
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            <EditFrom ref="findForm" daoName={daoName} controls={controlsFilter} clearItemMargin={true} record={{ qygm: 'all' }} selectFirst={false} />
          </div>
          <INDI_QTZB_DIVI_FHQK ref="divi" jlid={record.ID} ywid={11} title={"【" + record.Khmc + "】 业绩分配"} showEdit={showEdit} parentModel={daoName} />
          <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record
  }
}
INDI_FANA_SALE = connect(mapStateToProps)(INDI_FANA_SALE)
export default INDI_FANA_SALE;
let render = (props) => React.createElement(INDI_FANA_SALE, props);
let f = new Factory()
f.set("INDI_FANA_SALE", render)